package com.djzhou.gmms.role.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.department.TbDepartment;
import com.djzhou.gmms.model.role.TbRole;
import com.opensymphony.xwork2.ModelDriven;

public class RoleAction extends BaseAction<TbRole> implements ModelDriven<TbRole> {
	
	private TbRole role;
	private Integer roleId;
	private List<TbRole> list;
	
	private List<TbDepartment> departmentList;
	private Integer departmentId;
	private String roleIds;
	private Integer mainRoleId;
	
	@Override
	public String delete() {
		roleService.delete(roleId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("角色管理", queryUser(),"删除",roleId,"TbRole",super. queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		TbDepartment department=departmentService.queryById(departmentId);
		role.setDepartment(department);
		role.setCompany(department.getCompany());
		role.setLastModifyTime(new Date());
		role.setUser(queryUser());
		role.setDeleteFlag(0);
		roleService.update(role);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("角色管理", queryUser(),"编辑",roleId,"TbRole",super. queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		if(loadData==null)
			return SUCCESS;
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		if(departmentId!=null && !departmentId.equals("1") && !departmentId.equals(""))hashMap.put("departmentId", departmentId);
		hashMap.put("companyId", queryCompany().getCompanyId());
		pageBean=roleService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean=roleService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbRole>();
		}
		totalPage=pageBean.getTotalPage();
		setDepartmentId(departmentId);
		return SUCCESS;
	}
	
	public String queryRoleListByRoleIds(){
		list=roleService.queryList(roleIds);
		return SUCCESS;
	}

	@Override
	public String save() {
		if(role!=null){
			TbDepartment department=departmentService.queryById(departmentId);
			role.setDepartment(department);
			Date date=new Date();
			role.setCompany(department.getCompany());
			role.setCreateTime(date);
			role.setLastModifyTime(date);
			role.setDeleteFlag(0);
			role.setUser(queryUser());
			roleService.add(role);
			logService.addLog("角色管理",queryUser(),"添加",roleId,"TbRole",queryCompany(),super.queryDepartment());
		}
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		role=roleService.queryById(roleId);
		return SUCCESS;
	}

	public TbRole getModel() {
		return role;
	}

	public TbRole getRole() {
		return role;
	}

	public void setRole(TbRole role) {
		this.role = role;
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public List<TbRole> getList() {
		return list;
	}

	public void setList(List<TbRole> list) {
		this.list = list;
	}

	public List<TbDepartment> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<TbDepartment> departmentList) {
		this.departmentList = departmentList;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public Integer getMainRoleId() {
		return mainRoleId;
	}

	public void setMainRoleId(Integer mainRoleId) {
		this.mainRoleId = mainRoleId;
	}
	
	
}
